package com.zhiteng.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zhiteng.entity.CourseNode;
import com.zhiteng.entity.CourseTable;
import com.zhiteng.entity.SysDict;
import com.zhiteng.entity.TimeTable;
import com.zhiteng.entity.WeekAndCourseNum;
import com.zhiteng.entity.WeekAndDayAndCourseNum;
import com.zhiteng.service.CourseTableService;
import com.zhiteng.service.SysDictService;
import com.zhiteng.service.TimeTableService;
import com.zhiteng.utils.common.DateUtil;
import com.zhiteng.utils.common.ZTResult;

@RestController
@RequestMapping("/Substitute")
public class SubstituteController {

	@Autowired
	private CourseTableService courseTableService;

	@Autowired
	private SysDictService sysDictService;

	@Autowired
	private TimeTableService timeTableService;

	/**
	 * 查询调课原因类型
	 * 
	 * @return
	 */
	@RequestMapping("/querySysDict")
	@ResponseBody
	public ZTResult<List<SysDict>> querySysDict(@RequestParam("schoolId") String schoolId) {

		List<SysDict> types = sysDictService.querySysDict(schoolId);
		if (types.isEmpty() || types.size() == 0) {
			return ZTResult.result(1, "失败,请先去设置代课的原因");
		}
		return ZTResult.result(0, "成功", types);
	}

	/**
	 * 查询时间根据老师的名字和课程
	 * 
	 * @return
	 */
	@RequestMapping("/queryTimeByTeacherNameAndKC2")
	@ResponseBody
	public ZTResult<Set<WeekAndDayAndCourseNum>> queryTimeByTeacherNameAndKC(
			@RequestParam(value = "teacherId") String teacherId, @RequestParam("schoolId") String schoolId) {

//		List<CourseTable> courseTableList = courseTableService.queryCourseTableBySchoolIdAndTeacherId(schoolId,
//				teacherId,"2");
		List<CourseTable> courseTableList = courseTableService.queryCourseTableBySchoolIdAndTeacherId2(schoolId,
				teacherId, "2");
		if (courseTableList.isEmpty() || courseTableList.size() == 0) {
			return ZTResult.result(1, "更具学校的id和老师的id查询CourseTable失败");
		}
		// 这是要上课的老师的课表
		List<CourseNode> CourseNodeList = new ArrayList<>();
		for (CourseTable courseTable : courseTableList) {
			String node_ids = courseTable.getNode_ids();
			String[] split = node_ids.split(",");
			for (int i = 0; i < split.length; i++) {
				String CourseNodeId = split[i];
				CourseNode courseNode = courseTableService.queryCourseNodeByCourseNodeId(CourseNodeId);
				CourseNodeList.add(courseNode);
			}
		}
		List<WeekAndCourseNum> list4 = new ArrayList<>();
		for (CourseNode courseNode : CourseNodeList) {
			WeekAndCourseNum weekAndCourseNum = new WeekAndCourseNum();
			weekAndCourseNum.setWeek(courseNode.getWeek());
			weekAndCourseNum.setCourseNum(courseNode.getCourse_num());
			list4.add(weekAndCourseNum);
		}

		Map<String, Integer> map = new HashMap<>();
		map.put("星期一", Calendar.MONDAY);
		map.put("星期二", Calendar.TUESDAY);
		map.put("星期三", Calendar.WEDNESDAY);
		map.put("星期四", Calendar.THURSDAY);
		map.put("星期五", Calendar.FRIDAY);
		map.put("星期六", Calendar.SATURDAY);
		map.put("星期七", Calendar.SUNDAY);

		Calendar calendar = Calendar.getInstance();// 日历对象
		calendar.setTime(new Date());// 设置当前日期
		int yearStr = calendar.get(Calendar.YEAR);// 获取年份

		Set<WeekAndDayAndCourseNum> list2 = new HashSet<WeekAndDayAndCourseNum>();

		// 获取那个老师一个星期没有上课的时间课表
		// 获取整个班级的课表
		List<CourseTable> list3 = courseTableService.queryCourseTableByClassNameAndSchoolNameAndGradeName(
				CourseNodeList.get(0).getClass_name(), schoolId, CourseNodeList.get(0).getGrade_name());
		List<CourseNode> CourseNodeList2 = new ArrayList<>();
		for (CourseTable courseTable : list3) {
			String node_ids = courseTable.getNode_ids();
			String[] split = node_ids.split(",");
			for (int i = 0; i < split.length; i++) {
				String CourseNodeId = split[i];
				CourseNode courseNode = courseTableService.queryCourseNodeByCourseNodeId(CourseNodeId);
				CourseNodeList2.add(courseNode);
			}
		}

		List<WeekAndCourseNum> list5 = new ArrayList<>();
		// 比较获取那个老师没有上课的课表
		for (CourseNode courseNode : CourseNodeList2) {
			WeekAndCourseNum weekAndCourseNum = new WeekAndCourseNum();
			weekAndCourseNum.setWeek(courseNode.getWeek());
			weekAndCourseNum.setCourseNum(courseNode.getCourse_num());
			list5.add(weekAndCourseNum);
		}

		boolean b = list5.removeAll(list4);

		for (WeekAndCourseNum courseNode : list5) {
			List<String> list = DateUtil.getDays(yearStr, map.get(courseNode.getWeek()));// 得到所有的时间
//			List<Integer> Course_nums = courseTableService.queryCourseNumByTeacherNameAndKCAndWeek(schoolId,
//					teacherName, KC, courseNode.getWeek());
//			查询所有没有上课的节次,当做代课
//			List<Integer> Course_nums = courseTableService.queryCourseNumByTeacherIdAndWeek(schoolId, teacherId,
//					courseNode.getWeek());

//			for (Integer Course_num : Course_nums) {
			for (String day : list) {
				WeekAndDayAndCourseNum laobao = new WeekAndDayAndCourseNum();
				laobao.setWeek(courseNode.getWeek());
				laobao.setDay(day);
				laobao.setCourse_num(courseNode.getCourseNum());
				list2.add(laobao);
			}
		}
//		}

		return ZTResult.result(0, "成功", list2);
	}

	/**
	 * 设置一级二级科目
	 * 
	 * @param TimeTable
	 * @return
	 */
	@RequestMapping("saveTimeTable")
	@ResponseBody
	public ZTResult<Void> saveTimeTable(TimeTable TimeTable) {

		Set<String> TimeTable2 = timeTableService.queryAllTimeTable(TimeTable.getSchoolId());
		if (!TimeTable2.contains(TimeTable.getSubject())) {
			return ZTResult.result(1, "设置一级二级科目失败,科目名字填的不对");
		}

		List<TimeTable> TimeTableList = timeTableService.queryTimeTableBySubject(TimeTable.getSubject(),
				TimeTable.getSchoolId());
		if (TimeTableList != null && !TimeTableList.isEmpty()) {
			return ZTResult.result(1, "设置一级二级科目失败,科目已经被设置过啦,请先删除设置过得科目");
		}

		int i = timeTableService.saveTimeTable(TimeTable);
		if (i != 0) {
			return ZTResult.result(0, "设置一级二级科目成功");
		}
		return ZTResult.result(1, "设置一级二级科目失败");
	}

	/**
	 * 通过学校id查询科目
	 * 
	 * @param TimeTable
	 * @return
	 */
	@RequestMapping("queryAllTimeTable")
	@ResponseBody
	public ZTResult<List<TimeTable>> queryAllTimeTable(@RequestParam("schoolId") String schoolId) {

		Set<String> TimeTable = timeTableService.queryAllTimeTable(schoolId);

		timeTableService.deleteTimeTable(schoolId);
		for (String subject : TimeTable) {
			TimeTable table = new TimeTable();
			table.setSchoolId(schoolId);
			table.setSubject(subject);
			table.setType(1);
			int i = timeTableService.saveTimeTable(table);
		}
		List<TimeTable> TimeTable3 = timeTableService.queryAllTimeTable3(schoolId);
		return ZTResult.result(0, "通过学校id查询科目成功", TimeTable3);
	}

	/**
	 * 通过学校id和科目名删除科目
	 * 
	 * @param TimeTable
	 * @return
	 */
	@RequestMapping("deleteTimeTableBySubject")
	@ResponseBody
	public ZTResult<Void> deleteTimeTableBySubject(@RequestParam("schoolId") String schoolId,
			@RequestParam("subject") String subject) {

		int i = timeTableService.deleteTimeTableBySubject(schoolId, subject);

		if (i != 0) {
			return ZTResult.result(0, "通过学校id和科目名删除科目成功");
		}
		return ZTResult.result(1, "通过学校id和科目名删除科目失败");
	}

	/**
	 * 修改科目的状态
	 * 
	 * @param TimeTable
	 * @return
	 */
	@RequestMapping("updateTimeTableType")
	@ResponseBody
	public ZTResult<List<TimeTable>> updateTimeTableType(TimeTable TimeTable) {
		if (TimeTable.getType() == 1) {
			TimeTable.setType(2);
			int i = timeTableService.updateTimeTableType(TimeTable);
			if (i == 0) {
				return ZTResult.result(1, "修改科目的状态失败");
			}
		} else {
			TimeTable.setType(1);
			int i = timeTableService.updateTimeTableType(TimeTable);
			if (i == 0) {
				return ZTResult.result(1, "修改科目的状态失败");
			}
		}
		return ZTResult.result(0, "通过学校id和科目名删除科目成功");
	}
}
